﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraFollow : MonoBehaviour
{
    public EntityBase Player;


    //moving restriction
    public float Max_Horizontal, Min_Horizontal, Max_Vertical, Min_Vertical;

    private Transform playerPosition;
    private Transform cameraPosition;
    private Vector3 deltaTransform;

    public Vector3 pt;
    public void Start()
    {
        Player = GameObject.FindGameObjectWithTag("Player").transform.GetComponent<EntityBase>();


    }

    public void Update()
    {
        playerPosition = Player.transform;
        cameraPosition = this.transform;
        cameraPosition.position = new Vector3(Mathf.Clamp(cameraPosition.position.x, Min_Horizontal, Max_Horizontal),
                                              Mathf.Clamp(cameraPosition.position.y, Min_Vertical, Max_Vertical),
                                              -10f);

        //deltaTransform = playerPosition.position - cameraPosition.position;
        //deltaTransform.z = 0;
        //gameObject.transform.position += deltaTransform ;
        if (playerPosition.position.x < Min_Horizontal)
        {
            cameraPosition.position = new Vector3(Min_Horizontal, playerPosition.position.y, -10f);
            //cameraPosition.position += new Vector3(0, , 0);
        }
        if (playerPosition.position.x > Max_Horizontal)
        {
            cameraPosition.position = new Vector3(Max_Horizontal, playerPosition.position.y, -10f);
            //cameraPosition.position += new Vector3(0, playerPosition.position.y, 0);
        }
        if (playerPosition.position.y < Min_Vertical)
        {
            cameraPosition.position = new Vector3(playerPosition.position.x, Min_Vertical, -10f);
            //cameraPosition.position += new Vector3(,0 , 0);
        }
        if (playerPosition.position.y > Max_Vertical)
        {
            cameraPosition.position = new Vector3(playerPosition.position.x, Max_Vertical, -10f);
            //cameraPosition.position += new Vector3(playerPosition.position.x, 0 , 0);
        }
        if (playerPosition.position.x < Min_Horizontal && playerPosition.position.y < Min_Vertical)
        {
            cameraPosition.position = new Vector3(Min_Horizontal, Min_Vertical, -10f);
        }
        if (playerPosition.position.x > Max_Horizontal && playerPosition.position.y > Max_Vertical)
        {
            cameraPosition.position = new Vector3(Max_Horizontal, Max_Vertical, -10f);
        }
        if (playerPosition.position.x < Min_Horizontal && playerPosition.position.y > Max_Vertical)
        {
            cameraPosition.position = new Vector3(Min_Horizontal, Max_Vertical, -10f);
        }
        if (playerPosition.position.x > Max_Horizontal && playerPosition.position.y < Min_Vertical)
        {
            cameraPosition.position = new Vector3(Max_Horizontal, Min_Vertical, -10f);
        }
        if (playerPosition.position.x > Min_Horizontal && playerPosition.position.x < Max_Horizontal && playerPosition.position.y > Min_Vertical && playerPosition.position.y < Max_Vertical)
        {
            gameObject.transform.position = new Vector3(playerPosition.position.x, playerPosition.position.y, -10f);
        }
        pt = playerPosition.position;
    }

}
